<?php
namespace Admin\Controller;
use Common\Controller\CoreController;
// use Think\Controller;
class CategoryController extends CoreController {
    public function index(){//显示列表页
    	$user=M('category');
		$this->categorylist=$user->select();//从数据库保存取出，在index页面显示；OK
    	$this->assign('text','添加分类');
    	// $r=$this->getCateByPid();
    	$this->tdstr=$this->getCateTableHtml();
    	// $tdstr=$this->assign('tdstr',$t);
    	$this->assign('delCategory',U('delCategory'));
    	$this->assign('del',U('del'));
    	$this->assign('addCategory',U('addCategory'));
    	$this->display();
    }



    public function getCateByPid ($pid=0){
		// global $mysql;
		// $sql ="SELECT * FROM category WHERE status=1 and pid={$pid}";
		// $result = $mysql->getAll($sql);
		$user=M('Category');
		$result=$user->where("status=1 and precategory={$pid}")->select();
		if(!empty($result)){
			foreach ($result as $key => $value) {
				$result[$key]['child']=$this->getCateByPid($value['id']);
			}
		}
		return $result;
    }

	public function getCateOptionHtml($selectId=0,$cate=array()){
		static $optionStr;
		if(empty($cate)){
		$cate = $this->getCateByPid(0);
		}
		foreach ($cate as $key => $value) {
		if(is_array($selectId)){
		if(in_array($value['id'],$selectId)){
		$optionStr = $optionStr.'<option value="'.$value['id'].'" selected="selected">';
		}else{
		$optionStr = $optionStr.'<option value="'.$value['id'].'">';
		}
		}else{
		if($selectId==$value['id']){
		$optionStr = $optionStr.'<option value="'.$value['id'].'" selected="selected">';
		}else{
		$optionStr = $optionStr.'<option value="'.$value['id'].'">';
		}
		}

		for ($i=1; $i <$value['level'] ; $i++) { 
		$optionStr = $optionStr.'&nbsp;&nbsp;&nbsp;';
		}
		if($value['level']==0){
		$optionStr = $optionStr.$value['catename'].'</option>';
		}else{
		$optionStr = $optionStr.'└─'.$value['catename'].'</option>';
		}

		if(!empty($value['child'])){
		$this->getCateOptionHtml($selectId,$value['child']);
		}

		}
		return $optionStr;
	}

public function getCateTableHtml($cate=array()){
static $listStr;
// print_r($cate);
if(empty($cate)){
$cate =$this-> getCateByPid(0);
}
foreach ($cate as $key => $value) {
// print_r($value);
$listStr = $listStr.'<tr><td  class="col-md-1" >'.$value['id'].'</td><td  class="col-md-6" >';
for ($i=1; $i <$value['level'] ; $i++) { 
$listStr = $listStr.'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
}
if($value['level']==1){
$listStr = $listStr.$value['catename'].'</td>';
}else{
$listStr = $listStr.'└─'.$value['catename'].'</td>';
}
$listStr = $listStr.'<td>'.$value['status'].'</td>';
$listStr=$listStr.'<td  class="col-md-1"  style="text-align:center;"><a href="form.php?id='.$value['id'].'"><button class="btn btn-success "><span class="glyphicon glyphicon-cog"></span>查看分类名称</button></a</td>';
$listStr = $listStr.'<td  class="col-md-1"  style="text-align:center;"><a href="'.'editCategory'.'?editid='.$value['id'].'"><button class="btn btn-success "><span class="glyphicon glyphicon-cog"></span>编辑</button></a</td>';
$listStr = $listStr.'<td  class="col-md-1" ><a href="'.'delCategory'.'?delid='.$value['id'].'"  ><button class="btn btn-danger " onclick="return del();"><span class="glyphicon glyphicon-trash"></span>删除</button></a</td></tr>';

if(!empty($value['child'])){
$this->getCateTableHtml($value['child']);
}

}
return $listStr;
}

    public function addCategory(){
	header('Content-type:text/html;charset=utf-8');
    	if(IS_POST){
    		$user=M('category');
			$catename=I('post.catename');//获取分类名字，判断是否和数据库已经有
			$keyword=I('post.keyword');
			$description=I('post.description');
			$precategory=I('post.precategory');
			$status=I('post.status');
			if(empty($catename)&&empty($keyword)&&empty($description)&&empty($precategory)){
				$this->error('请输入完整的分类信息','addCategory');
			}

			$data=array(
				'catename'=>I('post.catename'),
				'keyword'=>I('post.keyword'),
				'description'=>I('post.description'),
				'precategory'=>I('post.precategory'),
				
				'status'=>I('post.status'),
				'add_time'=>date('Y-m-d H:i:s',time()+8*3600)
				);

			$r=$user->where("catename='{$catename}'")->find();
			// print_r($r);//OK
			if(I('post.precategory')==0){
				$data['level']=1;
			}else{
				$level=$user->where("id=".I('post.precategory'))->find();
				// print_r($level);
				// exit;
				$data['level']=$level['level']+1;
			}
			if(empty($r)){
				if($user->add($data)){
					header("location:index");//保存成功直接返回到index界面
					// $this->success('保存成功','index');
				}else{
					$this->error('保存失败','addCategory');
				}
			}else{
				$this->error('该分类已存在','addCategory');
			}
    	}else{
    		// $r=$this->getCateByPid();
    		// print_r($re);
    		// exit;
    		$re=$this->getCateOptionHtml();//值为0，默认去找所有的
    		$this->assign('str',$re);    		
    		// var_dump($re);
    		// exit;
    		$this->action=U('addCategory');
			$this->cancle=U('index');
	        $this->display();
    	}
    }

    public function editCategory(){
    	if(!is_null(I('get.editid'))){
    		$id=I('get.editid');
    		$user=M('category');
    		$r=$user->where('id='.$id)->find();
    		if(IS_POST){
    			header('Content-type:text/html;charset=utf-8');
    			// $this->action=U('editCategory');
    			$data=array(
    			'catename'=>I('post.catename'),
				'keyword'=>I('post.keyword'),
				'description'=>I('post.description'),
				'precategory'=>I('post.precategory'),			
				'status'=>I('post.status'),
    			);
    			$r=$user->where("id='{$id}'")->save($data);
    			if($r){
    				header("location:index");
    			}else{
    				$this->error('操作失败');
    			}
    		}else{
    			
    			// $this->assign('catelist',$r);
    			$this->assign('catelist',$r);
    			$user=M('category');
    			$r=$user->where("id='{$id}'")->select();
    			foreach ($r as $key => $value) {
    				$c[]=$value['precategory'];//获取父亲ID
    			}

	    		$re=$this->getCateOptionHtml($c);//值为0，默认去找所有的
	    		$this->assign('str',$re);
	    		$this->display();
    		}
    		
    	}
    	
    }

   

    public function delCategory(){
    	if(isset($_GET['delid'])){
    		$id=$_GET['delid'];//OK
    		$user=M('category');
    		$r=$user->where("id='{$id}' or precategory ='{$id}'")->delete();
    		if($r){
    			header("location:index");
    		}else{
    			$this->error('删除失败','index');
    		}
    	}
    }

} 